//TÖSIN HUUTOPUSSI BY VILLE & JUHO


/*
{maa:'hearts', value:6},{maa:'hearts', value:7},{maa:'hearts', value:8},{maa:'hearts', value:9},{maa:'hearts', value:10},{maa:'hearts', value:'jack'},{maa:'hearts', value:'queen'},{maa:'hearts', value:'king'},{maa:'hearts', value:'ace'},
{maa:'diamonds', value:6},{maa:'diamonds', value:7},{maa:'diamonds', value:8},{maa:'diamonds', value:9},{maa:'diamonds', value:10},{maa:'diamonds', value:'jack'},{maa:'diamonds', value:'queen'},{maa:'diamonds', value:'king'},{maa:'diamonds', value:'ace'},
{maa:'clubs', value:6},{maa:'clubs', value:7},{maa:'clubs', value:8},{maa:'clubs', value:9},{maa:'clubs', value:10},{maa:'clubs', value:'jack'},{maa:'clubs', value:'queen'},{maa:'clubs', value:'king'},{maa:'clubs', value:'ace'},
{maa:'spades', value:6},{maa:'spades', value:7},{maa:'spades', value:8},{maa:'spades', value:9},{maa:'spades', value:10},{maa:'spades', value:'jack'},{maa:'spades', value:'queen'},{maa:'spades', value:'king'},{maa:'spades', value:'ace'});
*/

var korttipakka = new Array();
var tick = new Array();
var players = new Array();

// create object array korttipakka with fields maa and value representing a deck of cards
for (var i=0; i<4; i++) {
	for (var j=6; j<15; j++) {
		korttipakka.push({maa:i,value:j});
	}
}

// return string representation for field value when value = [11,14]
function valueToString(value) {
	switch(value) {
		case 11:
			return 'jack'; break;
		case 12:
			return 'queen'; break;
		case 13:
			return 'king'; break;
		case 14:
			return 'ace'; break;
		default:
			return value; break;
	}
}

// returns string representation of field maa
function maaToString(maa) {
	switch(maa) {
		case 0:
			return 'hearts'; break;
		case 1:
			return 'diamonds'; break;
		case 2:
			return 'clubs'; break;
		case 3:
			return 'spades'; break;
	}
}

// returns string representation of card using valueToString and maaToString
function cardToString(card) {
	return valueToString(card.value) + " of " + maaToString(card.maa);
}

// returns "id string" of card (maa.value)
function ids(card) {
	return card.maa+"."+card.value;
}

// sorts by 1. field maa 2. field value -- in ascending order
function handSort(a,b) {
	if (a.maa < b.maa) { return -1; }
	else if (a.maa > b.maa) { return 1; }
	else {
		if (a.value < b.value) { return -1; }
		else if (a.value > b.value) { return 1; }
		else { return 0; }
	}
}

// sorts by field value in descending order
function valueSort(a,b) {
	if (a.value < b.value) { return 1; }
	else if (a.value > b.value) { return -1; }
	else { return 0; }
}

// returns <div id={parameter}>
function getDiv(c) {
	return "<div id=" + c + ">";
}

function rmById(id) {
	// epic oneliner
	document.getElementById(id).parentNode.removeChild(document.getElementById(id));
}

// called when a card button is pressed (logically, card is played)
// inserts card given as parameter to tick
function playCard(s) {

	if (tick.length == 3) { tick = new Array(); } // reset tick

	tick.push(s); // add to tick

	var m = 0;
	$.each(players[s.owner-1].hand, function(i,v) {
		if (v.maa == s.maa && v.value == s.value) {
			m = i;
		}
	});

	players[s.owner-1].hand.splice(m,1); // remove from JS array (by index)
	rmById(ids(s)); // remove from DOM hand (by id)


	$('#tick').append("played: " + JSON.stringify(s) + "<br/>"); // output log

	// check if pushed card was third
	if (tick.length == 3) {
		tick = tick.sort(valueSort); // sort by value 
		$('#tick').append("WINNER: " + tick[0].owner + "<br/>SORTEDTICK: " + JSON.stringify(tick)+"<br/>"); // output log
		players[tick[0].owner-1].ticks.push(tick);
		$("#p"+tick[0].owner+"ticks").append(JSON.stringify(players[tick[0].owner-1].ticks));
	}
}


$(document).ready(function(){ 
	/*
	PAKKA ARRAYN TESTAUS:
	
	for(var i=0; i<korttipakka.length; i++) {
		var obj = korttipakka[i];
		$('#javascript').append(obj.value + ' of ' + obj.maa + "<br/>");
    }
	$('#javascript').append("<br/>Number of cards: " + korttipakka.length + "<br/><br/>");
	*/

	//deal cards:
	   
	for (var p = 1; p < 4; p++) {
	
		var hand = new Array();
		for (var i=0; i<11; i++){
			//Math.floor((Math.random()*korttipakka.length)+0);
			var randomCard = Math.floor((Math.random()*korttipakka.length)+0);
			korttipakka[randomCard].owner = p; // add owner
			hand.push(korttipakka[randomCard]); // add random card to hand
			korttipakka.splice(randomCard,1); // remove card from deck
		}
		hand = hand.sort(handSort); // sort cards in hand
		var k = new Array();
		players.push({"hand":hand,"ticks":k}); // remember hand


		// print hand & boutons

		// player anchor
		var id = "p" + p;
		$('#javascript').append(getDiv(id));

		// heading
		$('#'+id).append("<p><b>THIS IS PLAYER " + p + " HAND:</b></p>");

		// print cards (as buttons)
		for (var i=0; i < hand.length; i++) {
			var obj = hand[i];

			var card = document.createElement('button');
			card.setAttribute('id', obj.maa+"."+obj.value);//ids(obj));
			card.setAttribute('onClick', 'playCard('+JSON.stringify(obj)+')'); // there is possibly a better way to pass json as parameter

			card.innerHTML = cardToString(obj);
			$('#'+id).append(card);
		}

		// anchor for ticks
		var td = document.createElement('div');
		td.setAttribute('id', 'p'+p+'ticks');
		$('#'+id).append(td);

		// heading
		$('#p'+p+'ticks').append("<p>tix</p>");

	}
	
	//PIRUNPAKKA:

	var dd = document.createElement('div');
	dd.setAttribute('id','dd');
	$('#javascript').append(dd);
	$('#dd').append("<p><b>PIRUNPAKKA:</b></p>");

	for(var i=0; i<korttipakka.length; i++) {
		var obj = korttipakka[i];
		var a = document.createElement('button');
		a.setAttribute('id', ids(obj));
		a.innerHTML = cardToString(obj);
		$('#dd').append(a);
    }

	//$('#tick').append(JSON.stringify(players));

});

